package com.university.trade.controller.admin;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.university.trade.entity.Blog;
import com.university.trade.entity.Userinfo;
import com.university.trade.redis.RedisClient;
import com.university.trade.service.SystemService;
import com.university.trade.service.UserinfoService;
import com.university.trade.util.ExportWordUtil;
import com.university.trade.util.PropertiesUtil;
import com.university.trade.util.RegExpValidatorUtils;
import com.university.trade.util.RequestUtil;
import com.university.trade.util.ResponseUtil;
import com.university.trade.util.SendMailUtil;
import com.university.trade.util.StringUtil;
import com.university.trade.util.SystemConstantData;
/**
 * 用户信息控制层
 * @author yaojinshan
 * 2016-10-14 14:11
 *
 */
@Controller
@RequestMapping("/userInfo")
public class UserInfoController {
	@Resource
	private SystemService systemService;
	@Resource
	private UserinfoService userinfoService;
	@Resource
	private RedisClient redisClient;
	/**
	 * 模版方法 在此基础上开发
	 * 规则：
	 * 		1.必带request,response参数.
	 * 		2.方法注释必须要写,关键的业务代码要加上注释
	 * 		3.加上作者和时间
	 * add by yaojinshan 2016-10-15 11:33
	 * @param request 请求
	 * @param response 响应
	 * @throws Exception 异常
	 */
	@RequestMapping("/test")
	public void list(HttpServletRequest request,HttpServletResponse response) throws Exception{
		Map<String,Object> resultMap =  RequestUtil.verifyRequest(request);
		//如果返回结果为F,说明请求不正确,直接返回resultMap;如果返回结果为T,说明请求正常,做具体的局业务处理
		if(SystemConstantData.RESULT_OK.equals(resultMap.get("result"))){
			// 具体业务处理
			
		}
		ResponseUtil.printJson(resultMap, response);
		return;
	}
	/**
	 * 发送验证消息方法
	 * add by yaojinshan 2016-10-15 11:33
	 * @param request
	 * @param response
	 * @param phone 待验证手机号
	 * @throws Exception
	 */
	@RequestMapping("/sendRegisterMessage")
	public void sendRegisterMessage(HttpServletRequest request,HttpServletResponse response,String phone) throws Exception{
		Map<String,Object> resultMap =  RequestUtil.verifyRequest(request);
		if(SystemConstantData.RESULT_OK.equals(resultMap.get("result"))){
			//1.验证用户手机号是否为空
			if(StringUtil.isEmpty(phone)){
				resultMap.put(SystemConstantData.PROMPT_MESSAGE, "手机号不能为空！");
				ResponseUtil.printJson(resultMap, response);
				return;
			}
			phone = phone.trim();
			//2.验证用户手机号格式是否正确
			if(!RegExpValidatorUtils.IsMobilePhone(phone)){
				resultMap.put(SystemConstantData.PROMPT_MESSAGE, "手机号格式不正确！");
				ResponseUtil.printJson(resultMap, response);
				return;
			}
			//3.验证用户手机号是否被注册
			if(!checkUserExist(phone)){
				resultMap.put(SystemConstantData.PROMPT_MESSAGE, "该手机号已经被注册！");
				ResponseUtil.printJson(resultMap, response);
				return;
			}
			//4.发送验证短信,有效期为五分钟,初始密码为123456
			PropertiesUtil systemProperties = new PropertiesUtil("/systemConfig.properties");
			String system_message_validtime = systemProperties.getProperty("system_message_validtime");
			String system_open_test = systemProperties.getProperty("system_open_test");//是否打开测试环境
			int sendMessageResult = 0;
			String randomNum = "";
			if(!"T".equals(system_open_test)){
				randomNum = ResponseUtil.randomNum(6);
				//非测试环境，需要发短信
				sendMessageResult = systemService.sendRegisterMessage();
				if(sendMessageResult > 0){
					resultMap.put(SystemConstantData.PROMPT_MESSAGE, "本次验证码为"+randomNum+"已发送到你手机，有效时间"+system_message_validtime+"分钟!");
				}
			}else{
				//非测试环境，不需要发短信
				randomNum = "123456";
				resultMap.put(SystemConstantData.PROMPT_MESSAGE, "本次验证码为"+randomNum+",已发送到你手机，有效时间"+system_message_validtime+"分钟!");
			}
			ResponseUtil.printJson(resultMap, response);
			return;
		}
		ResponseUtil.printJson(resultMap, response);
		return;
	}
	/**
	 * 检查用户是否存在
	 * @param phone
	 * @return
	 */
	private boolean checkUserExist(String phone) {
		Userinfo userinfo = null;
		userinfo = userinfoService.selectByPrimaryKey(phone);
		if(userinfo == null){
			return true;
		}
		return false;
	}
	@RequestMapping("/registerUser")
	public void registerUser(HttpServletRequest request,HttpServletResponse response,Userinfo userInfo) throws Exception{
		Map<String,Object> resultMap =  RequestUtil.verifyRequest(request);
		if(SystemConstantData.RESULT_OK.equals(resultMap.get("result"))){
			// 具体业务处理
			//1.判断用户信息是否存
		}
		ResponseUtil.printJson(resultMap, response);
		return;
	}
	@RequestMapping("/addUser")
	public void addUser(HttpServletRequest request,HttpServletResponse response,Userinfo userInfo) throws Exception{
		Map<String,Object> resultMap =  RequestUtil.verifyRequest(request);
		if(SystemConstantData.RESULT_OK.equals(resultMap.get("result"))){
			// 具体业务处理
			//1.判断用户信息是否存
		}
		ResponseUtil.printJson(resultMap, response);
		return;
	}
	/**
	 * 测试redis
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@SuppressWarnings("rawtypes")
	@RequestMapping(value="testRedis")  
    public void testRedis(HttpServletRequest request,HttpServletResponse response) throws Exception{
    	//发送邮件demo
    	//EmailUtil.send("subject", "content", "924099504@qq.com");
		Map<String,Object> resultMap = new HashMap<String,Object>();
    	//redisClient.add("aaaa", "aaaa");
    	//System.out.println(redisClient.get("aaaa"));
    	
    	redisClient.setCacheObject("string", "stringValue");
    	System.out.println((String)redisClient.getCacheObject("string"));
    	
    	//redisClient.setCacheObject("integer", 22222);
    	//System.out.println((Integer)redisClient.getCacheObject("integer"));
    	
    	Blog blog = new Blog();
    	blog.setId(1);
    	redisClient.setCacheObject("blog", blog);
    	System.out.println((Blog)redisClient.getCacheObject("blog"));
    	
    	List<String> list = new ArrayList<String>();
    	list.add("111");
    	list.add("333");
    	
    	redisClient.setCacheList("arrayList", list);
    	System.out.println((List)redisClient.getCacheList("arrayList"));
    	
    	Map<String,String> map = new HashMap<String,String>();
    	map.put("mapKey", "mapValue");
    	redisClient.setCacheMap("mapCache", map);
    	System.out.println((Map)redisClient.getCacheMap("mapCache"));
    	
    	resultMap.put("aaaa", redisClient.getCacheObject("string"));
    	ResponseUtil.printJson(resultMap, response);
    	return;
    }
	/**
	 * 测试发送邮件
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value="testSendEmail")  
	public void testSendEmail(HttpServletRequest request,HttpServletResponse response) throws Exception{
		//发送邮件demo
		Map<String,Object> resultMap = new HashMap<String,Object>();
		String receiver = "869893469@qq.com";//测试邮箱地址
		String content = "测试邮件内容";//邮件内容
		String subject = "测试邮件主题";//邮件主题
		int sendResult = SendMailUtil.sendSingleEmail(receiver, content, subject);
		if(sendResult == 0){
			resultMap.put("sendMessage","发送失败");
		}else{
			resultMap.put("sendMessage","发送成功");
		}
		ResponseUtil.printJson(resultMap, response);
		return;
	}
	/**
	 * 测试word文档导出
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value="testExportWord")  
	public void testExportWord(HttpServletRequest request,HttpServletResponse response) throws Exception{
		//发送邮件demo
		Map<String,Object> resultMap = new HashMap<String,Object>();
		try {
			ExportWordUtil exportWordUtil = new ExportWordUtil();
			Map<String, Object> map = new HashMap<>();
			List<Userinfo> userList = new ArrayList<Userinfo>();
			Userinfo userinfo = null;
			userinfo = userinfoService.selectByPrimaryKey("13031000132");
			map.put("userinfo", userinfo);
			userList.add(userinfo);
			userList.add(userinfo);
			userList.add(userinfo);
			userList.add(userinfo);
			map.put("userList", userList);
			exportWordUtil.createDoc(map , "test_export_word.xml", "word文件导出测试.doc", request, response);
			resultMap.put("exportMessage","导出成功");
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("exportMessage","导出失败");
			ResponseUtil.printJson(resultMap, response);
		}
		return;
	}
}
